package com.chen.core.model;

import com.chen.core.enums.ResponseEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * 核心响应对象
 *
 * @author chenguangde
 * @version 1.0
 * @Description
 * @createTime 2021年05月11日 20:39:00
 */
@Data
@Schema(name = "核心响应对象")
public class CommonResponseBean<T> {
    /**
     * 响应码
     */
    @Schema(name = "responseCode", description = "响应码")
    private String responseCode;

    /**
     * 错误码
     */
    @Schema(name = "errorCode", description = "错误码")
    private String errorCode;

    /**
     * 详情
     */
    @Schema(name = "message", description = "详情")
    private String message;

    /**
     * 响应结果详情
     */
    @Schema(name = "data", description = "响应结果详情")
    private T data;

    /**
     * 获取成功响应对象
     */
    public static <T> CommonResponseBean<T> success() {
        return getResponseByEnum(ResponseEnum.SUCCESS);
    }

    /**
     * 获取成功响应对象
     */
    public static <T> CommonResponseBean<T> success(T data) {
        CommonResponseBean<T> responseByEnum = getResponseByEnum(ResponseEnum.SUCCESS);
        responseByEnum.setData(data);
        return responseByEnum;
    }

    /**
     * 获取失败响应对象
     */
    public static <T> CommonResponseBean<T> fail() {
        return getResponseByEnum(ResponseEnum.FAIL);
    }

    /**
     * 获取失败响应对象
     *
     * @param errorCode 错误码
     * @param message   详情
     */
    public static <T> CommonResponseBean<T> fail(String errorCode, String message) {
        CommonResponseBean<T> fail = fail();
        fail.setErrorCode(errorCode);
        fail.setMessage(message);
        return fail;
    }

    /**
     * 通过枚举获取响应对象
     *
     * @param responseEnum 响应枚举类
     */
    public static <T> CommonResponseBean<T> getResponseByEnum(ResponseEnum responseEnum) {
        CommonResponseBean<T> commonResponseBean = new CommonResponseBean<>();
        commonResponseBean.setResponseCode(responseEnum.getResponseCode());
        commonResponseBean.setErrorCode(responseEnum.getErrorCode());
        commonResponseBean.setMessage(responseEnum.getMessage());
        return commonResponseBean;
    }
}
